/*
 *	A 4 button slider.  There are some optimizations that need to be done. 
 *	Rob Ferrante, 2/92
 */
#import "GradeSlider.h"
#import <appkit/View.h>
#import <appkit/NXImage.h>
#import <dpsclient/wraps.h>
#import <appkit/Application.h>
#import <appkit/graphics.h>

@implementation GradeSlider


- initFrame: (NXRect *)r
{	
	NXSize  knobSize;
	
	self = [super initFrame:r];
	knobA = [[NXImage alloc] initFromSection:"A.tiff"];
	knobB = [[NXImage alloc] initFromSection:"B.tiff"];
	knobC = [[NXImage alloc] initFromSection:"C.tiff"];
	knobD = [[NXImage alloc] initFromSection:"D.tiff"];
	[knobA getSize:&knobSize];
	knobOff = knobSize.width/2.0;
	NXSetRect(&rectA, 90,0,knobSize.width, knobSize.height);
	NXSetRect(&rectB, 190,0,knobSize.width, knobSize.height);
	NXSetRect(&rectC, 290,0,knobSize.width, knobSize.height);
	NXSetRect(&rectD, 390,0,knobSize.width, knobSize.height);
	return self;
}

- setAction:(SEL)a
{
	action = a;
	return self;
}

- setTarget:(id)t
{
	target = t;
	return self;
}
- drawSelf:(NXRect *)list:(int)count
{
	PSsetgray(NX_DKGRAY);
	NXRectFill(&bounds);
	[knobA composite:NX_SOVER toPoint:&(rectA.origin)];
	[knobB composite:NX_SOVER toPoint:&(rectB.origin)];
	[knobC composite:NX_SOVER toPoint:&(rectC.origin)];
	[knobD composite:NX_SOVER toPoint:&(rectD.origin)];
	return self;
}

- mouseDown: (NXEvent *)e
{
	NXPoint	mLoc, oldLoc;
	NXRect  *currentKnobRect, oldRect, penRect = bounds;
	id  currentKnobImage;
	NXEvent	*nextEvent;
	int	oldMask, checkMask;
	
	
	oldMask = [window eventMask];
	checkMask = NX_MOUSEUPMASK | NX_MOUSEDRAGGEDMASK;
	[window setEventMask:( oldMask|checkMask)];
	oldLoc = e->location;
	[self convertPoint:&oldLoc fromView:nil];

/*	Check if the cursor clicked on a button, and set up if so  */

	if (NXMouseInRect(&oldLoc, &rectA, NO)) {
		currentKnobImage = knobA;
		currentKnobRect = &rectA;
		penRect.origin.x = bounds.origin.x;
		penRect.size.width = rectB.origin.x - penRect.origin.x;
		action = @selector(moveA:);
	} else if (NXMouseInRect(&oldLoc, &rectB, NO)) {	
		currentKnobImage = knobB;
		currentKnobRect = &rectB;
		penRect.origin.x = rectA.origin.x + rectA.size.width;
		penRect.size.width = rectC.origin.x - penRect.origin.x;
		action = @selector(moveB:);
	} else if (NXMouseInRect(&oldLoc, &rectC, NO)) {	
		currentKnobImage = knobC;
		currentKnobRect = &rectC;
		penRect.origin.x = rectB.origin.x + rectB.size.width;
		penRect.size.width = rectD.origin.x - penRect.origin.x;
		action = @selector(moveC:);
	} else if (NXMouseInRect(&oldLoc, &rectD, NO)) {	
		currentKnobImage = knobD;
		currentKnobRect = &rectD;
		penRect.origin.x = rectC.origin.x + rectC.size.width;
		penRect.size.width = bounds.size.width - penRect.origin.x;
		action = @selector(moveD:);
	} else return self;
	
	[self lockFocus];
	PSsetgray(NX_DKGRAY);

/*	Loop while dragging the slider */

	while ((nextEvent = [NXApp getNextEvent:checkMask])->type !=NX_MOUSEUP) {
		mLoc = nextEvent->location;
		[self convertPoint:&mLoc fromView:nil];
		oldRect = *currentKnobRect;
		NXOffsetRect(currentKnobRect, mLoc.x - oldLoc.x, 0);
		if ( NXContainsRect(&penRect, currentKnobRect)==YES) {
			NXRectFill(&oldRect);
			[currentKnobImage composite:NX_COPY toPoint:
				&currentKnobRect->origin];
			[window flushWindow]; // so cursor doesn't flicker
			[self  sendAction:action to:target];
			oldLoc = mLoc;
		} else *currentKnobRect =oldRect;
	}
	[self unlockFocus];
	[window setEventMask:oldMask];
	return self;
}


- (float)posA { return rectA.origin.x + knobOff; }
- (float)posB { return rectB.origin.x + knobOff; }
- (float)posC { return rectC.origin.x + knobOff; }
- (float)posD { return rectD.origin.x + knobOff; }

@end
